<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CompletableFuture 演示</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        h2 {
            color: #666;
            border-bottom: 2px solid #007bff;
            padding-bottom: 10px;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
            font-size: 14px;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
        .result {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            padding: 15px;
            margin-top: 10px;
            white-space: pre-wrap;
            font-family: monospace;
            font-size: 12px;
            max-height: 300px;
            overflow-y: auto;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }
        .success {
            color: #28a745;
        }
        .error {
            color: #dc3545;
        }
    </style>
</head>
<body>
    <h1>CompletableFuture 异步编程演示</h1>

    <div class="grid">
        <!-- 基本演示 -->
        <div class="container">
            <h2>1. 基本异步执行</h2>
            <p>演示 CompletableFuture 的基本用法，包括 runAsync 和 supplyAsync</p>
            <button onclick="basicDemo()">运行演示</button>
            <div id="basicResult" class="result" style="display: none;"></div>
        </div>

        <!-- 链式调用 -->
        <div class="container">
            <h2>2. 链式调用</h2>
            <p>演示 thenApply 链式调用，将多个异步操作串联起来</p>
            <button onclick="chainDemo()">运行演示</button>
            <div id="chainResult" class="result" style="display: none;"></div>
        </div>

        <!-- 组合任务 -->
        <div class="container">
            <h2>3. 组合异步任务</h2>
            <p>演示 thenCombine 组合多个并行执行的任务结果</p>
            <button onclick="combineDemo()">运行演示</button>
            <div id="combineResult" class="result" style="display: none;"></div>
        </div>

        <!-- 异常处理 -->
        <div class="container">
            <h2>4. 异常处理</h2>
            <p>演示 exceptionally 和 handle 方法处理异步任务中的异常</p>
            <button onclick="exceptionDemo()">运行演示</button>
            <div id="exceptionResult" class="result" style="display: none;"></div>
        </div>

        <!-- 超时处理 -->
        <div class="container">
            <h2>5. 超时处理</h2>
            <p>演示使用 anyOf 实现任务超时控制</p>
            <button onclick="timeoutDemo()">运行演示</button>
            <div id="timeoutResult" class="result" style="display: none;"></div>
        </div>

        <!-- 订单处理 -->
        <div class="container">
            <h2>6. 订单处理流程</h2>
            <p>演示完整的电商订单处理流程</p>
            <div class="form-group">
                <label for="orderId">订单ID:</label>
                <input type="text" id="orderId" value="ORDER-001">
            </div>
            <div class="form-group">
                <label for="productName">商品名称:</label>
                <input type="text" id="productName" value="iPhone 15">
            </div>
            <div class="form-group">
                <label for="quantity">数量:</label>
                <input type="number" id="quantity" value="2" min="1">
            </div>
            <div class="form-group">
                <label for="price">单价:</label>
                <input type="number" id="price" value="5999" min="0" step="0.01">
            </div>
            <button onclick="processOrder()">处理订单</button>
            <div id="orderResult" class="result" style="display: none;"></div>
        </div>

        <!-- 批量订单处理 -->
        <div class="container">
            <h2>7. 批量订单处理</h2>
            <p>演示并行处理多个订单</p>
            <div class="form-group">
                <label for="batchSize">订单数量:</label>
                <input type="number" id="batchSize" value="5" min="1" max="20">
            </div>
            <button onclick="batchProcessOrders()">批量处理</button>
            <div id="batchResult" class="result" style="display: none;"></div>
        </div>
    </div>

    <script>
        const API_BASE = '/api/completable-future';

        async function basicDemo() {
            const button = event.target;
            const resultDiv = document.getElementById('basicResult');
            
            button.disabled = true;
            button.textContent = '执行中...';
            resultDiv.style.display = 'block';
            resultDiv.textContent = '正在执行基本异步演示...';
            
            try {
                const response = await fetch(`${API_BASE}/basic-demo`);
                const result = await response.json();
                resultDiv.textContent = JSON.stringify(result, null, 2);
            } catch (error) {
                resultDiv.textContent = `错误: ${error.message}`;
                resultDiv.className = 'result error';
            } finally {
                button.disabled = false;
                button.textContent = '运行演示';
            }
        }

        async function chainDemo() {
            const button = event.target;
            const resultDiv = document.getElementById('chainResult');
            
            button.disabled = true;
            button.textContent = '执行中...';
            resultDiv.style.display = 'block';
            resultDiv.textContent = '正在执行链式调用演示...';
            
            try {
                const response = await fetch(`${API_BASE}/chain-demo`);
                const result = await response.json();
                resultDiv.textContent = JSON.stringify(result, null, 2);
            } catch (error) {
                resultDiv.textContent = `错误: ${error.message}`;
                resultDiv.className = 'result error';
            } finally {
                button.disabled = false;
                button.textContent = '运行演示';
            }
        }

        async function combineDemo() {
            const button = event.target;
            const resultDiv = document.getElementById('combineResult');
            
            button.disabled = true;
            button.textContent = '执行中...';
            resultDiv.style.display = 'block';
            resultDiv.textContent = '正在执行组合任务演示...';
            
            try {
                const response = await fetch(`${API_BASE}/combine-demo`);
                const result = await response.json();
                resultDiv.textContent = JSON.stringify(result, null, 2);
            } catch (error) {
                resultDiv.textContent = `错误: ${error.message}`;
                resultDiv.className = 'result error';
            } finally {
                button.disabled = false;
                button.textContent = '运行演示';
            }
        }

        async function exceptionDemo() {
            const button = event.target;
            const resultDiv = document.getElementById('exceptionResult');
            
            button.disabled = true;
            button.textContent = '执行中...';
            resultDiv.style.display = 'block';
            resultDiv.textContent = '正在执行异常处理演示...';
            
            try {
                const response = await fetch(`${API_BASE}/exception-demo`);
                const result = await response.json();
                resultDiv.textContent = JSON.stringify(result, null, 2);
            } catch (error) {
                resultDiv.textContent = `错误: ${error.message}`;
                resultDiv.className = 'result error';
            } finally {
                button.disabled = false;
                button.textContent = '运行演示';
            }
        }

        async function timeoutDemo() {
            const button = event.target;
            const resultDiv = document.getElementById('timeoutResult');
            
            button.disabled = true;
            button.textContent = '执行中...';
            resultDiv.style.display = 'block';
            resultDiv.textContent = '正在执行超时处理演示...';
            
            try {
                const response = await fetch(`${API_BASE}/timeout-demo`);
                const result = await response.json();
                resultDiv.textContent = JSON.stringify(result, null, 2);
            } catch (error) {
                resultDiv.textContent = `错误: ${error.message}`;
                resultDiv.className = 'result error';
            } finally {
                button.disabled = false;
                button.textContent = '运行演示';
            }
        }

        async function processOrder() {
            const button = event.target;
            const resultDiv = document.getElementById('orderResult');
            
            const orderData = {
                orderId: document.getElementById('orderId').value,
                productName: document.getElementById('productName').value,
                quantity: parseInt(document.getElementById('quantity').value),
                price: parseFloat(document.getElementById('price').value)
            };
            
            button.disabled = true;
            button.textContent = '处理中...';
            resultDiv.style.display = 'block';
            resultDiv.textContent = '正在处理订单...';
            
            try {
                const response = await fetch(`${API_BASE}/order-processing`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(orderData)
                });
                const result = await response.json();
                resultDiv.textContent = JSON.stringify(result, null, 2);
            } catch (error) {
                resultDiv.textContent = `错误: ${error.message}`;
                resultDiv.className = 'result error';
            } finally {
                button.disabled = false;
                button.textContent = '处理订单';
            }
        }

        async function batchProcessOrders() {
            const button = event.target;
            const resultDiv = document.getElementById('batchResult');
            const batchSize = parseInt(document.getElementById('batchSize').value);
            
            // 生成测试订单
            const orders = [];
            for (let i = 1; i <= batchSize; i++) {
                orders.push({
                    orderId: `ORDER-${String(i).padStart(3, '0')}`,
                    productName: `商品${i}`,
                    quantity: Math.floor(Math.random() * 5) + 1,
                    price: Math.floor(Math.random() * 1000) + 100
                });
            }
            
            button.disabled = true;
            button.textContent = '处理中...';
            resultDiv.style.display = 'block';
            resultDiv.textContent = `正在批量处理 ${batchSize} 个订单...`;
            
            try {
                const response = await fetch(`${API_BASE}/batch-order-processing`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ orders })
                });
                const result = await response.json();
                resultDiv.textContent = JSON.stringify(result, null, 2);
            } catch (error) {
                resultDiv.textContent = `错误: ${error.message}`;
                resultDiv.className = 'result error';
            } finally {
                button.disabled = false;
                button.textContent = '批量处理';
            }
        }
    </script>
</body>
</html> 